package com.gym.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.gym.pojo.Catsaletake;
import com.gym.pojo.Cattype;
import com.gym.pojo.Member;
import com.gym.pojo.User;
import com.gym.service.MemberService;
import com.gym.service.UserService;
import com.gym.utils.Different;
import com.gym.vo.CatsaletakeVo;
import com.gym.vo.Caum;
import com.gym.vo.UserVo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class MemberController {

    @Resource
    private MemberService memberService;

    @Resource
    private UserService userService;

    @RequestMapping("/listMem")//传入会员名字和catId
    public List<UserVo> listMem(String name,Integer catId){
        if (name==null||name==""||name.equals("")){
            name="";
        }
        if (catId==null){
            catId=0;
        }
        return memberService.listMem(name,catId);
    }

    @RequestMapping("/selListMember")
    public JSONObject selMember(String name,Integer catId,int index,int size){
        if (name==null||name==""||name.equals("")){
            name="";
        }
        if (catId==null){
            catId=0;
        }
        PageInfo<UserVo> page=memberService.selListMember(name,catId,index,size);
        JSONObject json=new JSONObject();
        json.put("code",0);
        json.put("msg","");
        json.put("count",page.getTotal());
        json.put("data",page.getList());
        return json;
    }

    @RequestMapping("/expireMem")//传入两个时间
    public List<UserVo> expireMem(String date,String time){
        return  memberService.expireMem(date,time);
    }

    @RequestMapping("/updMemStatic")
    public boolean updMemStatic(int memId) {
        return memberService.updMemStatic(memId);
    }

    @RequestMapping("/listCat")
    public List<Cattype> listCat(String name){return memberService.listCat(name);}

    @RequestMapping("/delCatById")
    public boolean delCatById(int catId) {
        return memberService.delCatById(catId);
    }

    @RequestMapping("/selCatByMem")
    public boolean selCatByMem(int catId) {
        return memberService.selCatByMem(catId);
    }

    @RequestMapping("/updCatById")
    public boolean updCatById(@RequestBody Cattype cat) {
        return memberService.updCatById(cat);
    }

    @RequestMapping("/insCat")
    public boolean insCat(@RequestBody Cattype cat) {
        return memberService.insCat(cat);
    }

    @RequestMapping("/insertMem")
    @Transactional
    public boolean inMem(@RequestBody Caum caum) throws ParseException {
        Calendar date=Calendar.getInstance();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        User user=caum.getUser();//用户
        Cattype cat=caum.getCat();//会员卡
        Catsaletake cst=caum.getCst();//会员购买/续卡
            cst.setSaledate(sdf.parse(sdf.format(date.getTime())));
        Member mem=new Member();
        mem.setUserId(user.getUserId());//用户编号
        mem.setStatureTime(sdf.parse(sdf.format(date.getTime())));//当前时间
        mem.setStatus(1);//会员状态
        mem.setCattype(cat.getCatId());//会员卡编号
        mem.setMoney(0);//会员余额

        Date time=null;
        if (cat.getValiddays()==30){
            time= Different.getDateAfter(date.getTime(),30);
        }else if (cat.getValiddays()==90){
            time= Different.getDateAfter(date.getTime(),90);
        }else if (cat.getValiddays()==365){
            time= Different.getDateAfter(date.getTime(),365);
        }else {
             time= Different.getDateAfter(date.getTime(),7);
        }
        mem.setExpires(time);//会员过期时间
        if (memberService.saveMem(mem)&&memberService.saveCst(cst)){
            if (userService.UpdMemIdById(user.getUserId())){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }

    @RequestMapping("/insCatSaleTake")
    public boolean insCatSaleTake(@RequestBody Catsaletake cst){
        Date date=new Date();
        cst.setSaledate(date);
        System.out.println(cst);
        return memberService.saveCst(cst);
    }

    @RequestMapping("/selCatSelaTake")
    public JSONObject selCatSelaTake(String date, String time,int index,int size) throws ParseException {
        PageInfo<CatsaletakeVo> page=memberService.selCatSaleTake(date,time,index,size);
        JSONObject json=new JSONObject();
        json.put("code",0);
        json.put("msg","");
        json.put("count",page.getTotal());
        json.put("data",page.getList());
        return json;
    }

    @RequestMapping("/selUserById")
    public UserVo selUserById(int userId) {
        return memberService.selUserById(userId);
    }

    @RequestMapping("/continuous")
    public boolean continuous(@RequestBody Caum caum){
        return memberService.continuous(caum.getCst(),caum.getMem(),caum.getCat());
    }
}
